import { Tooltip, Button, Input, Form, Col, Row, Select, Checkbox,Radio } from "antd";
import eventBus, { useSubscribe } from "../../../framework/common/eventBus";

//几个审批类型公用的字段
import getInitFormData from "./getInitFormData"; //获取初始化表单数据
import InitiaterOrg from "../components/InitiaterOrg"; //发起组织
import Opinion from "../components/Opinion"; //审批意见
import CClist from "../components/CClist"; //抄送人
import SignField from "../components/SignField"; //签章
import SelectAuditors from "../../../RuntimeForm/SelectAuditors/SelectAuditors";


//审批同意表单
export default function ({ tplData, urlPrefix, formRef, approveType = "AddSign", approveText }) {
  const [form] = Form.useForm(); //表单对象
  formRef.current = form; //穿透form对象给父组件

  //表单默认值
  if (!tplData.__approveForm__[approveType]) {
    tplData.__approveForm__[approveType] = getInitFormData({
      tplData: tplData,
      fields: {
        opinion: "", //审批意见
        initiater_org_id: "", //审批组织id
        initiater_org_name: "", //审批组织中文名
        add_sign_users: [], //加签人
        add_sign_after: "0", // xx后
        sign_id: tplData.ProfileSign?.default_sign?.id, //签章
      },
    });
  }

  //设置发起表单数据，用于ant表单更新后，反向更新tplData内发起表单的数据
  function updateTplData(data) {
    tplData.__approveForm__[approveType] = {
      ...tplData.__approveForm__[approveType],
      ...data,
    };
  }

  //console.log(approveType + "初始化表单数据",tplData.__approveForm__[approveType])

  return (
    <Form
      form={form}
      name="basic"
      labelAlign={"left"}
      colon={false}
      labelCol={{
        flex: "80px",
      }}
      onValuesChange={(value, allValues) => {
        //回填表单数据到tplData
        //比较特殊需要额外回填的，单独到组件内的change事件处理，比如发起组织
        updateTplData(allValues);
      }}
      initialValues={tplData.__approveForm__[approveType]}
      autoComplete="off"
    >
      <Form.Item
        label="审批意见"
        name="opinion"
        rules={[
          {
            required: true,
            message: "",
          },
        ]}
      >
        <Opinion approveType={approveType} placeholder={"请输入审批意见"}></Opinion>
      </Form.Item>
      <Form.Item
        className="mb-10px"
        label="审批组织"
        name="initiater_org_id"
        rules={[
          {
            required: true,
            message: "",
          },
        ]}
      >
        <InitiaterOrg approveType={approveType} form={form} updateTplData={updateTplData}></InitiaterOrg>
      </Form.Item>
      <Form.Item
        className="mb-10px"
        label={approveText+"人"}
        name="add_sign_users"
        rules={[
          {
            required: true,
            message: "",
          },
        ]}
      >
        <SelectAuditors
          textField={"auditor_name"}
          valueField={"auditor_id"}
          CurUserGUID={tplData.CurUserGUID}
          package_id={tplData.package_id}
          CurUserDepartmentGUID={tplData.CurUserDepartmentGUID}
          urlPrefix={urlPrefix}
          placeholder={"请选择"+approveText+"人"}
          title={approveText+"人"}
        ></SelectAuditors>
      </Form.Item>

      <Form.Item
        className="mb-10px"
        label={approveText+"后"}
        name="add_sign_after"
      >
        <Radio.Group name="add_sign_after">
          <Radio value="0">需要我再次确认</Radio>
          <Radio value="1">进入下一步审批</Radio>
        </Radio.Group>
      </Form.Item>
      {tplData.ProfileSign?.enable_sign == 1 && (
        <Form.Item
          className="mb-0"
          label="选择签章"
          name="sign_id"
          rules={[
            {
              required: true,
              message: "",
            },
          ]}
        >
          <SignField tplData={tplData}></SignField>
        </Form.Item>
      )}
    </Form>
  );
}
